package com.tucanstubs.android_wheresmystuff.activities;

import java.util.ArrayList;
import java.util.List;

import com.tucanstubs.android_wheresmystuff.R;
import com.tucanstubs.android_wheresmystuff.login.User;
import com.tucanstubs.android_wheresmystuff.database.ItemHelper;
import com.tucanstubs.android_wheresmystuff.database.UserHelper;
import com.tucanstubs.android_wheresmystuff.items.*;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Spinner;

/**
 * Activity for displaying a list of items. Items can be clicked on to bring up a screen displaying
 * information specific to the item.
 * 
 * @author team 37
 * @version 2.0 4/9/2013
 *
 */
public class ItemListActivity extends Activity implements OnItemSelectedListener {
	
	private final ItemListActivity currentActivity = this;
	private List<Item> values;
	private User user;
	private Spinner spinner;
	private String prevClass;
	private ListView itemListView;
	private ListAdapter adapter;
	private List<Item> itemList;
	
	/**
	 * Creates the activity. Initializes all GUI Components 
	 * and retrieves information form the bundle.
	 * The activity receives a bundle with the keys Tags.USERNAME,Tags.ITEMIDS, 
	 * and Tags.CLASS.
	 * Tags.USERNAME maps to the user name of the currently logged in user. Tags.ITEMIDS maps
	 * to an int[] of item ids. These are the items to be displayed. Tags.CLASS maps
	 * to the name of the previous activity, this will be passed on to ItemInfoActivity to
	 * determine whether search or claim needs to be displayed.
	 * 
	 * @param SavedInstanceState not used
	 */
	public void onCreate(Bundle SavedInstanceState){
		super.onCreate(SavedInstanceState);
		setContentView(R.layout.items_list_screen);
		Bundle bundle=getIntent().getExtras();
		int[] itemIDs = bundle.getIntArray(Tags.ITEMIDS);
		prevClass = bundle.getString(Tags.CLASS);
		user=UserHelper.get(this, bundle.getString(Tags.USERNAME));
		//get items from their ids
		itemList = new ArrayList<Item>();
		for (int i: itemIDs) {
			itemList.add(ItemHelper.getItem(this, i));
		}
		
		//populate list
		values = (List<Item>)itemList;
		 itemListView = (ListView) findViewById(R.id.list);
		 adapter = new ArrayAdapter<Item>(this,
				  android.R.layout.simple_list_item_1, android.R.id.text1, values);
		itemListView.setAdapter(adapter);
		
		//populates spinner and sets its onclick method
		addItemsOnSpinner();
		addListenerOnSpinnerItemSelection();


		itemListView.setOnItemClickListener(new OnItemClickListener() {
			/**
			 * Allows user to click on an item and be taken to the item's info 
			 * screen
			 */
			public void onItemClick(AdapterView<?> parent, View v, int pos,
					long id) {
				
				Intent nextIntent = new Intent(currentActivity, ItemInfoActivity.class);
				Bundle bundle = new Bundle();
				bundle.putInt(Tags.ITEM,values.get(pos).getId());
				bundle.putString(Tags.CLASS,prevClass);
				bundle.putString(Tags.USERNAME, user.getUserName());
				nextIntent.putExtras(bundle);
				startActivity(nextIntent);
				Log.i(Tags.ITEMLISTACTIVITY, "You pressed " + pos);
			}
			
		});
	}
	
	
    
    //SPINNER methods
    
    /**
     * Method to add the values to be displayed onto the spinner
     */
    public void addItemsOnSpinner() {
    	spinner = (Spinner) findViewById(R.id.Filter_Spinner);
    	List<String> list = new ArrayList<String>();
    	list.add("All");
    	list.add("Recent");
    	list.add("Oldest");
    	list.add("Claimed");
    	list.add("Unclaimed");
    	list.add("Lost");
    	list.add("Found");
    	list.add("Donation");
    	list.add("Request");
    	ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
    		android.R.layout.simple_spinner_item, list);
    	dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	spinner.setAdapter(dataAdapter);
    	Log.i(Tags.ITEMLISTACTIVITY,"Result in adapter"+dataAdapter.getItem(2));
      }
    
    
    /**
     * Method for setting the listener for the spinner, in this case upon selected item
     */
	public void addListenerOnSpinnerItemSelection() {
	     spinner = (Spinner) findViewById(R.id.Filter_Spinner);
	     spinner.setOnItemSelectedListener(this);
    }
	

	/**
	 * Updates the item list to be filtered based on the select filter from
	 * the spinner
	 * 
	 */
	public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
		// An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
		Log.i(Tags.ITEMLISTACTIVITY,"selected filter: "+pos);
		values = Filter.filter(itemList,pos);
		 itemListView = (ListView) findViewById(R.id.list);
		 adapter = new ArrayAdapter<Item>(this,
				  android.R.layout.simple_list_item_1, android.R.id.text1, values);
		itemListView.setAdapter(adapter);
	}
	 /**
	  * When nothing is selected do nothing.
	  * @param arg0 not used
	  */
	public void onNothingSelected(AdapterView<?> arg0) {
		 // Another interface callback
	}
}
